/*
 * car_test.c
 *
 *  Created on: 21/04/2011
 *      Author: Yotam and Gilad
 */

#include "../car.h"
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "test_utilities.h"

#define ZERO 0

bool testCarCreate() {
	Car newCar = carCreate(1234, "Chevrolet", 100, 10);
	ASSERT_TEST(carGetLicensePlate(newCar) == 1234);
	ASSERT_TEST(strcmp(carGetName(newCar),"Chevrolet") == ZERO);
	ASSERT_TEST(carGetPrice(newCar) == 100);
	ASSERT_TEST(carGetKm(newCar) == 10);
	carFree(newCar);
	return true;
}

bool testCarCopy() {
	Car fiatCopy = carCopy(NULL);
	ASSERT_TEST(fiatCopy == NULL);
	Car fiat = carCreate(444,"Fiat",20,46);
	fiatCopy = carCopy(fiat);
	ASSERT_TEST(carCompareByName(fiat, fiatCopy) == ZERO);
	ASSERT_TEST(carCompareByLicensePlate(fiatCopy,fiat) == ZERO);
	carFree(fiat);
	carFree(fiatCopy);
	return true;
}

bool testCarCompareByName() {
	Car car1 = carCreate(234111, "Audi", 2000, 1);
	Car car2 = carCreate(234112, "Limuzin", 1400, 5);
	Car car3 = carCreate(234113, "Lincoln", 1000, 9);
	Car car4 = carCreate(234114, "Lincoln", 1002, 11);
	ASSERT_TEST(carCompareByName(car1, car2) < ZERO);
	ASSERT_TEST(carCompareByName(car2,car3) < ZERO);
	ASSERT_TEST(carCompareByName(car4,car3) == ZERO);
	ASSERT_TEST(carCompareByName(car3,car2) > ZERO);
	carFree(car1);
	carFree(car2);
	carFree(car3);
	carFree(car4);
	return true;
}

bool testCarCompareByKm() {
	Car car1 = carCreate(234111, "Mazda", 2000, 1);
	Car car2 = carCreate(234112, "Chevrolet", 1400, 5);
	Car car3 = carCreate(234113, "Volvo", 1000, 9);
	Car car4 = carCreate(234114, "Volvo", 1000, 9);
	ASSERT_TEST(carCompareByKm(car1, car2) > ZERO);
	ASSERT_TEST(carCompareByKm(car2,car3) > ZERO);
	ASSERT_TEST(carCompareByKm(car4,car3) == ZERO);
	ASSERT_TEST(carCompareByKm(car3,car2) < ZERO);
	carFree(car1);
	carFree(car2);
	carFree(car3);
	carFree(car4);
	return true;
}

bool testCarCompareByLicensePlate() {
	Car car1 = carCreate(234111, "Mazda",2000,1);
	Car car2 = carCreate(234112, "Chevrolet",1400, 5);
	Car car3 = carCreate(234123, "Jaguar",1000,9);
	Car car4 = carCreate(234123, "Volvo",1200,111);
	ASSERT_TEST(carCompareByLicensePlate(car3,car4) == ZERO);
	ASSERT_TEST(carCompareByLicensePlate(car2,car1) > ZERO);
	ASSERT_TEST(carCompareByLicensePlate(car2,car3) < ZERO);
	carFree(car1);
	carFree(car2);
	carFree(car3);
	carFree(car4);
	return true;
}

bool testCarFilterNotInUse() {
	Car newCar = carCreate(1234, "Chevy", 20, 10);
	ASSERT_TEST(carFilterNotInUse(newCar));
	carSetUseMode(newCar, true);
	ASSERT_TEST(!carFilterNotInUse(newCar));
	carFree(newCar);
	return true;
}

bool testCarFree() {
	Car newCar = carCreate(7777, "Mazda", 234, 17);
	carFree(newCar);
	carFree(NULL);
	return true;
}

bool testCarGetLicensePlate() {
	Car newCar = carCreate(12345, "Mazda", 234, 17);
	ASSERT_TEST(carGetLicensePlate(newCar) == 12345);
	carFree(newCar);
	return true;
}

bool testCarGetPrice() {
	Car newCar = carCreate(18345, "Chevrolet", 865, 2);
	ASSERT_TEST(carGetPrice(newCar) == 865);
	carFree(newCar);
	return true;
}

bool testCarGetSalePrice() {
	Car newCar = carCreate(19652, "Nissan", 999, 5);
	carSale(newCar,5,10,40);
	ASSERT_TEST(carGetSalePrice(newCar) == 40);
	carFree(newCar);
	return true;
}

bool testCarGetName() {
	Car newCar = carCreate(123468, "Fiat", 9871, 12);
	ASSERT_TEST(strcmp(carGetName(newCar), "Fiat") == ZERO);
	carFree(newCar);
	return true;
}

bool testCarGetKm() {
	Car newCar = carCreate(1112, "Toyota", 9321, 11);
	ASSERT_TEST(carGetKm(newCar) == 11);
	carFree(newCar);
	return true;
}

bool testCarGetSaleKmMin() {
	Car newCar = carCreate(7875, "Honda", 56738, 234);
	carSale(newCar, 240, 260, 40);
	ASSERT_TEST(carGetSaleKmMin(newCar) == 240);
	carFree(newCar);
	return true;
}

bool testCarGetSaleKmMax() {
	Car newCar = carCreate(3888, "Subaro", 3939, 123);
	carSale(newCar, 120, 140, 50);
	ASSERT_TEST(carGetSaleKmMax(newCar) == 140);
	carFree(newCar);
	return true;
}

bool testCarGetCarInUse() {
	Car newCar = carCreate(4442, "Mazda", 6664, 21);
	carSetUseMode(newCar,true);
	ASSERT_TEST(carGetCarInUse(newCar) == true);
	carFree(newCar);
	return true;
}

bool testCarSale() {
	Car car1 = carCreate(12345,"BMW", 28903,2);
	Car car2 = carCreate(12346,"Mazda", 2432,9);
	carSetUseMode(car2,true);
	Car car3 = carCreate(74594,"Mercedes",56784,10);
	carSale(car3, 5, 15, 20);
	Car car4 = carCreate(83748,"Toyota",2, 24);
	Car car5 = carCreate(75392,"Chevrolet",100, 20);
	ASSERT_TEST(carSale(car2,5,27,30) == CAR_IN_USE);
	ASSERT_TEST(carSale(car1,0,18,6) == CAR_INVALID_KM);
	ASSERT_TEST(carSale(car1,12,10,20) == CAR_INVALID_KM);
	ASSERT_TEST(carSale(car1,6,18,0) == CAR_INVALID_PRICE);
	ASSERT_TEST(carSale(car3,4,25,16) == CAR_ALREADY_ON_SALE);
	ASSERT_TEST(carSale(car4,25,45,80) == CAR_SALE_PRICE_TOO_BIG);
	ASSERT_TEST(carSale(car5,21,35,30) == CAR_SUCCESS);
	carFree(car1);
	carFree(car2);
	carFree(car3);
	carFree(car4);
	carFree(car5);
	return true;
}

bool testCarNoSale() {
	Car car1 = carCreate(74746,"Mazda",5555,10);
	carSale(car1, 10, 18, 15);
	Car car2 = carCreate(84382,"Opel", 7777,16);
	carSetUseMode(car2, true);
	Car car3 = carCreate(72954,"Honda",6666,15);
	ASSERT_TEST(carNoSale(car1) == CAR_SUCCESS);
	ASSERT_TEST(carNoSale(car2) == CAR_IN_USE);
	ASSERT_TEST(carNoSale(car3) == CAR_NO_SALE);
	carFree(car1);
	carFree(car2);
	carFree(car3);
	return true;
}

bool testCarGetSaleStatus() {
	Car car1 = carCreate(9090, "Mazda", 3478, 8);
	carSale(car1, 11, 18, 20);
	ASSERT_TEST(carGetSaleStatus(car1) == true);
	Car car2 = carCreate(2425, "Mazda", 2626, 10);
	carSale(car2, 12, 15, 0);
	ASSERT_TEST(carGetSaleStatus(car2) == false);
	carFree(car1);
	carFree(car2);
	return true;
}

bool testCarSetUseMode() {
	Car car1 = NULL;
	Car car2 = carCreate(23456, "Toyota", 5455, 45);
	ASSERT_TEST(carSetUseMode(car1,true) == CAR_NULL_ARGUMENT);
	ASSERT_TEST(carSetUseMode(car2,true) == CAR_SUCCESS);
	carFree(car2);
	return true;
}

int main() {
	RUN_TEST(testCarCreate);
	RUN_TEST(testCarCopy);
	RUN_TEST(testCarCompareByName);
	RUN_TEST(testCarCompareByKm);
	RUN_TEST(testCarCompareByLicensePlate);
	RUN_TEST(testCarFilterNotInUse);
	RUN_TEST(testCarFree);
	RUN_TEST(testCarGetLicensePlate);
	RUN_TEST(testCarGetPrice);
	RUN_TEST(testCarGetSalePrice);
	RUN_TEST(testCarGetName);
	RUN_TEST(testCarGetKm);
	RUN_TEST(testCarGetSaleKmMin);
	RUN_TEST(testCarGetSaleKmMax);
	RUN_TEST(testCarGetCarInUse);
	RUN_TEST(testCarSale);
	RUN_TEST(testCarNoSale);
	RUN_TEST(testCarGetSaleStatus);
	RUN_TEST(testCarSetUseMode);
	return 0;
}

